package com.infore.airsupervise.service;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import com.infore.airsupervise.bean.UserE;

import java.util.List;

/**
 * 任务调度服务
 * <p>
 * 作者: dingxinlin
 * 创建日期: 2017/4/22 14:15
 */

@SuppressLint("NewApi")
public class JobHandleService extends JobService {

    private int kJobId = 0;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    /**
     * 被启动
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        scheduleJob(getJobInfo());
        return START_NOT_STICKY;
    }

    @Override
    public boolean onStartJob(JobParameters params) {

        // 用户未登录时停止service
        if (!UserE.getLoginStatus()) {
            stopSelf();
            return false;
        }

        boolean isMainServiceWork = isServiceWork(this, "com.infore.airsupervise.service.MainService");
        boolean isSubServiceWork = isServiceWork(this, "com.infore.airsupervise.service.SubService");

        if (!isMainServiceWork || !isSubServiceWork) {
            this.startService(new Intent(this, MainService.class));
            this.startService(new Intent(this, SubService.class));
        }

        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        scheduleJob(getJobInfo());
        return false;
    }


    /**
     * 发送job到JobScheduler
     * @param t
     */
    public void scheduleJob(JobInfo t) {
        JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.schedule(t);
    }

    /**
     * 获取JobInfo
     * @return
     */
    public JobInfo getJobInfo() {
        JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName(this, JobHandleService.class));
        // 任何网络状态下有效
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        // 手机重启后有效
        builder.setPersisted(true);
        // 非充电状态下有效
        builder.setRequiresCharging(false);
        // 手机闲置时有效
        builder.setRequiresDeviceIdle(false);
        // 指定job的时间间隔
        builder.setPeriodic(1);

        return builder.build();
    }

    /**
     * 判断某服务正在运行
     * @param mContext
     * @param serviceName
     * @return
     */
    public boolean isServiceWork(Context mContext, String serviceName) {
        boolean isWork = false;

        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runList = activityManager.getRunningServices(100);

        if (runList.size() <= 0) {
            return false;
        }

        for (int i = 0; i < runList.size(); i++) {
            String mName = runList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }

        return isWork;
    }
}
